package com.cskaoyan.service;

import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketGoodsExample;
import com.cskaoyan.bean.vo.WxCatalogIndexVo;
import com.cskaoyan.bean.vo.wx_catalog_current.WxCatalogCurrentVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/08 17:43
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {


    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public WxCatalogCurrentVo selectById(Integer id) {
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        WxCatalogCurrentVo wxCatalogCurrentVo = new WxCatalogCurrentVo();
        wxCatalogCurrentVo.copyCurrentCategory(marketCategory);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(id);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        for (MarketCategory category : marketCategories) {
            wxCatalogCurrentVo.copyCurrentSubCategoryNew(category);

        }
        if (marketCategories.size() == 0) {

            MarketGoodsExample example = new MarketGoodsExample();
            example.createCriteria().andCategoryIdEqualTo(id);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
            for (MarketGoods marketGood : marketGoods) {
                wxCatalogCurrentVo.copyCurrentSubCategory(marketGood);
            }
        }

        return wxCatalogCurrentVo;
    }

    @Override
    public WxCatalogIndexVo selectAll() {
        MarketCategoryExample example = new MarketCategoryExample();
        WxCatalogIndexVo wxCatalogIndexVo = new WxCatalogIndexVo();
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        for (MarketCategory marketCategory : marketCategories) {
            wxCatalogIndexVo.copyCategoryList(marketCategory);
        }
        MarketCategory marketCategory = marketCategories.get(0);
        Integer id = marketCategory.getId();
        wxCatalogIndexVo.copyCurrentCategory(marketCategory);

        MarketCategoryExample example1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example1.createCriteria();
        criteria.andPidEqualTo(id);
        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(example1);
        for (MarketCategory category : marketCategories1) {
            wxCatalogIndexVo.copyCurrentSubCategory(category);
        }

        return wxCatalogIndexVo;
    }
}
